﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace MINERP.DAL
{
    public abstract class DALBase
    {
        private static newSampleYPDBEntities _dataEntites = null;

        public static newSampleYPDBEntities DataEntites
        {
            get
            {
                if (_dataEntites == null)
                {
                    _dataEntites = new newSampleYPDBEntities();

                }
                ConnStr = GetConnFromEntity();
                return _dataEntites;
            }
        }
        //       public void DALBase()
        //       {
        //ConnStr=GetConnFromEntity();
        //       }
        public static string ConnStr = GetConnFromEntity();
        public static void Save()
        {
            _dataEntites.SaveChanges();
        }

        public static void DeleteObject(object o)
        {
            DataEntites.DeleteObject(o);
        }
        public DataTable ExceSQL(string inSQL)
        {

            Database db = null;


            DataTable table = new DataTable();
            try
            {
                db = DatabaseFactory.CreateDatabase();

                DbCommand dbCommand = db.GetSqlStringCommand(inSQL);
                DataSet set = db.ExecuteDataSet(dbCommand);
                table = set.Tables[0];
            }
            catch
            {
                throw;
            }
            return table;
        }
        public DataSet ExceSQLDataSet(string inSQL)
        {

            Database db = null;

            DataSet set;

            try
            {
                db = DatabaseFactory.CreateDatabase();
                //db.ConnectionString = "";

                
                DbCommand dbCommand = db.GetSqlStringCommand(inSQL);
                dbCommand.CommandTimeout = 60 * 5;
                set = db.ExecuteDataSet(dbCommand);

            }
            catch
            {
                throw;
            }
            return set;
            //using (SqlConnection con = new SqlConnection(ConnStr))
            //{

            //}
        }

        public static string GetConnFromEntity()
        {
            string srcStr = System.Configuration.ConfigurationManager.ConnectionStrings["newSampleYPDBEntities"].ToString();
            string[] ArryConfig = srcStr.Split(';');

            string dataSource = ArryConfig.FirstOrDefault(p => p.Contains("Data Source")).ToString().Split('\"')[1];
            string dataBase = ArryConfig.FirstOrDefault(p => p.Contains("Initial Catalog")).ToString();
            string userid = ArryConfig.FirstOrDefault(p => p.Contains("User ID")).ToString();
            string pwd = ArryConfig.FirstOrDefault(p => p.Contains("Password")).ToString();
            //string pwd = ArryConfig[5].Trim('\\');
            string connStr = dataSource + ";" + dataBase + ";" + userid + ";" + pwd;

            return connStr;
        }
    }
}
